package cn.itcast.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String like = queryPageBean.getQueryString();
        Page<Permission> list = null;
        if (like != null) {
            list = this.permissionDao.findlike(like);
        } else {
            list = this.permissionDao.findAll();
        }
        return new PageResult(list.getTotal(), list.getResult());
    }


    @Override
    public void add(Permission permission) {
        this.permissionDao.add(permission);
    }

    @Override
    public void delete(Integer id) {
        this.permissionDao.delete(id);
    }

    @Override
    public void update(Permission permission) {
        this.permissionDao.update(permission);
    }

    //回显权限
    @Override
    public Permission findById(Integer id) {
        return this.permissionDao.findById(id);
    }

    /**
     * 根据菜单id查询权限信息
     *
     * @param menuIds
     * @return
     */
    @Override
    public List<Permission> findByMenuIdsWithPermission(Integer[] menuIds) {
        Integer[] permissionIds = this.permissionDao.findPermissionByMenu(menuIds);
        if (ObjectUtil.isEmpty(permissionIds)){
            return Collections.emptyList();
        }
        return this.permissionDao.findByMenuIdsWithPermission(permissionIds);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return this.permissionDao.findAll();
    }


}



